package com.zzyl.common.ai;

import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.ChatCompletion;
import com.openai.models.ChatCompletionCreateParams;
import com.openai.models.ResponseFormatJsonObject;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@AllArgsConstructor
@Slf4j
public class AIModelInvoker {

    private final AliBaiLianProperties aliBaiLianProperties;

    /**
     * 调用模型智能评估
     * @param prompt 提问的问题
     * @return
     */
    public String invoke(String prompt) {
        log.info("调用模型开始");
        // 创建OpenAIClient实例
        OpenAIClient client = OpenAIOkHttpClient.builder()

            .apiKey(aliBaiLianProperties.getApiKey())
            .baseUrl(aliBaiLianProperties.getUrl())
            .build();

        ChatCompletionCreateParams params = ChatCompletionCreateParams.builder()
            .addUserMessage(prompt)
            .responseFormat(ResponseFormatJsonObject.builder().build())//指定返回的数据格式为json
            .model(aliBaiLianProperties.getModelName())
            .build();
        ChatCompletion chatCompletion = client.chat().completions().create(params);

        String result = chatCompletion.choices().get(0).message().content().get();
        log.info("调用模型结束: {}", result);
        return result;
    }
}
